package org.aokey.springdemo2;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

public class JSONText {
    @Test
    public void JsontoJava() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        //定义一个JSON字符串
        String s="{\"name\":\"咪咪\",\"age\":1,\"color\":\"blue\"}";
        //转对象
        Animals cat=mapper.readValue(s,Animals.class);
        System.out.println(cat.toString());
    }
    @Test
    public void JavatoJson() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        //创建Java对象
        Animals cat = new Animals();
        cat.setName("cat");
        cat.setAge(1);
        cat.setColor("blue");
        //转换为JSON
        String str = mapper.writeValueAsString(cat);
        System.out.println(str);
    }
}
